﻿using System;
using System.Linq;

namespace Core.DatabaseHelpers
{
    public class ColumnAttribute:Attribute
    {
        public enum KeyType
        {
            Auto,
            FillIn
        }
        public enum DependentCounter
        {
            HasOne,HasMany
        }
        private string _through = "";
        private string _hasManyModelsFrom = "";
        private bool _is_lazy;
        private string _type;
        private bool _pk;
        private bool _isChild;
        private KeyType _keyType = KeyType.FillIn;

        public string Name { get; set; }

        public bool IsPK { get { return _pk; } }

        public bool IsFillIn
        {
            get { return _keyType == KeyType.FillIn; }
        }

        public bool IsChild
        {
            get { return _isChild; }
        }

        public string CategoryType
        {
            get { return _type; }
        }

        public string Through
        {
            get { return _through; }
        }

        public ColumnAttribute(string name)
        {
            Name = name;
            _pk = false;
        }

        public ColumnAttribute(string name, KeyType keyType)
        {
            Name = name;
            _pk = false;
            _keyType = keyType;
        }

        public ColumnAttribute(string name,  KeyType keyType,bool pk = false)
        {
            Name = name;
            _pk = pk;
            _keyType = keyType;
        }

        public ColumnAttribute(string name, bool isLasy = false, bool isChild = true, string childtable = "", string through = "", KeyType keyType = KeyType.FillIn)
        {
            _is_lazy = isLasy;
            Name = name;
            _keyType = keyType;
            _hasManyModelsFrom = childtable;
            _isChild = isChild;
            _through = through;
        }

        public ColumnAttribute(string name, bool isLasy = false, bool isChild = true, string childtable = "")
        {
            _is_lazy = isLasy;
            Name = name;

            _hasManyModelsFrom = childtable;
            _isChild = isChild;

        }

        public ColumnAttribute(string name, bool isLasy = false, bool isChild = true, string childtable = "", KeyType keyType = KeyType.FillIn)
        {
            _is_lazy = isLasy;
            Name = name;
            _keyType = keyType;
            _hasManyModelsFrom = childtable;
            _isChild = isChild;
            _pk = true;
        }

        public bool IsField 
        { 
            get{return _hasManyModelsFrom.Length == 0;}
        }

        public bool IsManyToMany
        {
            get { return _through.Length > 0; }
        }
       
    }
}
